home *** CD-ROM | disk | FTP | other *** search
- //this widget is a nice example of the joys and problems of wrapping a commandline tool...
- var file, nickname, username, password, init_path, address, server;
-
- function upload() {
- if (FTPDrop) {
- if (init_path.charAt(0) != "/") init_path = "/" + init_path;
- if (init_path.charAt(init_path.length-1) != "/") init_path += "/";
-
- //alert(init_path);
-
- var new_address = "ftp://" + address + init_path;
- alert(new_address);
- FTPDrop.uploadFile(file, new_address, username, password);
- }
- }
-
- function showProgress(progress) {
- progress = progress.substring(progress.lastIndexOf(" ")+1, progress.length);
- if (progress != "") document.getElementById("up_percentage").innerText = progress + "%";
- //the above happens sometimes...
- }
-
- //three functions used depending on curls exit status:
- function noHost() {
- drop_zone.style.backgroundImage = "url(images/connection_failed.png)";
- setTimeout(
- function() {
- toggleSettings();
- resetForSendingAgain();
- }, 1000);
- }
-
- function authFailed() {
- drop_zone.style.backgroundImage = "url(images/authentication_failed.png)";
- setTimeout(
- function() {
- toggleSettings();
- resetForSendingAgain();
- }, 1000);
- }
-
- function successfullUpload() {
- drop_zone.style.backgroundImage = "url(images/complete.png)";
- setTimeout(
- function() {
- resetForSendingAgain();
- }, 1000);
- }
-
- function resetForSendingAgain() {
- document.getElementById("info_display").innerText = "ftp to";
- document.getElementById("up_percentage").style.visibility = "hidden";
- document.getElementById("nickname_display").innerText = nickname;
- document.getElementById("up_percentage").innerText = "0%";
-
- drop_zone.style.backgroundImage = "url(images/dropHere.png)";
-
- drop_zone.addEventListener("drop", getFile, true);
- drop_zone.addEventListener("dragenter", doDragEnter, true);
- drop_zone.addEventListener("dragover", doDragMove, true);
- drop_zone.addEventListener("dragleave", doDragLeave, true);
-
- }
-
- function getFile(e) {
- file = getUnixFilePath(e.dataTransfer.getData("text/uri-list"));
- //alert(file);
- document.getElementById("info_display").innerText = "progress";
- document.getElementById("up_percentage").style.visibility = "visible";
- document.getElementById("nickname_display").innerText = getFileName(file);
-
- var drop_zone = document.getElementById("drop_zone");
-
- drop_zone.style.backgroundImage = "url(images/transferring.png)";
-
-
- drop_zone.removeEventListener("drop", getFile, true);
- drop_zone.removeEventListener("dragenter", doDragEnter, true);
- drop_zone.removeEventListener("dragover", doDragMove, true);
- drop_zone.removeEventListener("dragleave", doDragLeave, true);
-
- setTimeout("upload()", 0);
-
- e.preventDefault();
- e.stopPropagation();
- }
-
- function getUnixFilePath(file) {
- if (file.indexOf("file://localhost") != -1) file = file.substring(new String("file://localhost").length, file.length);
-
- //file = file.replace(/%20/g,"\\ ");
- file = file.replace(/%20/g," "); //plugin doesn't use escaped paths
- file = decodeURI(file);
- return file;
- }
-
- function getFileName(file) {
- var tmp = file.split("/");
-
- tmp = tmp[tmp.length-1];
- tmp = tmp.replace(/\\ /g, " ");
-
- return tmp;
- }
-
- //three functions needed to stop event propogation so we can capture the ondrop event
- //for the element we want.
- function doDragEnter (e) {
- e.target.style.backgroundImage = "url(images/dropHere_over.png)";
- e.preventDefault();
- e.stopPropagation();
- }
-
- function doDragLeave (e) {
- document.getElementById("drop_zone").style.backgroundImage = "url(images/dropHere.png)";
- e.preventDefault();
- e.stopPropagation();
- }
-
- function doDragMove (e) {
- e.preventDefault();
- e.stopPropagation();
- }
-
- function getSavedServers() {
- var servers_request = new XMLHttpRequest();
- servers_request.open("GET", "servers.xml", false);
- servers_request.send(null);
-
- return servers_request.responseXML;
- }
-
- function checkForSavedServer(name) {
- var data = getSavedServers();
- var servers = data.getElementsByTagName("server");
-
- for (var i=0; i < servers.length; i++)
- if (servers[i].getAttribute("nickname") == name) return true;
-
- return false;
- }
-
- function getSavedServerInfo(name) {
- var data = getSavedServers();
- var servers = data.getElementsByTagName("server");
-
- for (var i=0; i < servers.length; i++) {
- if (servers[i].getAttribute("nickname") == name) {
- nickname = servers[i].getAttribute("nickname");
- address = servers[i].getAttribute("address");
- username = servers[i].getAttribute("username");
- init_path = servers[i].getAttribute("path");
-
- //alert(nickname + ", " + address + ", " + username + ", " + init_path);
-
- document.getElementById("nickname").value = nickname;
- document.getElementById("username").value = username;
- document.getElementById("address").value = address;
- document.getElementById("path").value = init_path;
-
- document.getElementById("nickname_display").innerText = nickname;
-
- document.getElementById("password").focus();
-
- break;
- }
- }
- }
-
- function saveServer() {
- var data = getSavedServers();
- var servers = data.getElementsByTagName("server");
- var servers_length = data.getElementsByTagName("server").length;
-
- /*
- Have a new DOM document that we put all the info into, rather than manipulating var data.
- If we find it belwow, put it in (clone the node) then
- make the new ones for the devices we don't have.
- */
-
- var xml_doc = document.implementation.createDocument('', '', null);
- var servers_node = xml_doc.createElement("servers");
-
- for (var i=0; i < servers_length; i++) {
- if (servers[i].getAttribute("nickname") != nickname) {
- var s = xml_doc.createElement("server");
- s.setAttribute("nickname", servers[i].getAttribute("nickname"));
- s.setAttribute("address", servers[i].getAttribute("address"));
- s.setAttribute("username", servers[i].getAttribute("username"));
- s.setAttribute("path", servers[i].getAttribute("path"));
-
- servers_node.appendChild(s);
- }
- }
-
- //and now put in the server they've just added:
- var s = xml_doc.createElement("server");
- s.setAttribute("nickname", nickname);
- s.setAttribute("address", address);
- s.setAttribute("username", username);
- s.setAttribute("path", init_path);
-
- servers_node.appendChild(s);
-
- xml_doc.appendChild(servers_node);
-
- //alert('done');
-
- //and write the XML doc to our cache file:
- var xml_source = '<?xml version="1.0" encoding="UTF-8"?>\n';
- xml_source += new XMLSerializer().serializeToString(xml_doc);
-
- //alert('xml source: ' + xml_source);
-
- widget.system("/bin/rm servers.xml", null);
- widget.system("/bin/echo '" + xml_source + "' > servers.xml", null);
-
- //alert('file written');
- }
-
- function verifySettings() {
- nickname = document.getElementById("nickname"); //.value;
- username = document.getElementById("username"); //.value;
- password = document.getElementById("password"); //.value;
- address = document.getElementById("address"); //.value;
- init_path = document.getElementById("path"); //.value;
-
- var arrow = document.getElementById("arrow");
-
-
- nickname.style.background = "white";
- username.style.background = "white";
- password.style.background = "white";
- address.style.background = "white";
-
- if (nickname.value == "") {
- arrow.style.top = "43px";
- arrow.style.display = "block";
- nickname.style.background = "indianred";
- nickname.focus();
- return false;
- }
-
- if (address.value == "") {
- arrow.style.top = "82px";
- arrow.style.display = "block";
- address.style.background = "indianred";
- address.focus();
- return false;
- }
-
- if (username.value == "") {
- arrow.style.top = "122px";
- arrow.style.display = "block";
- username.style.background = "indianred";
- username.focus();
- return false;
- }
-
- if (password.value == "") {
- arrow.style.top = "122px";
- arrow.style.display = "block";
- password.style.background = "indianred";
- password.focus();
- return false;
- }
-
- nickname = nickname.value;
- username = username.value;
- password = password.value;
- address = address.value;
- init_path = init_path.value;
-
- arrow.style.display = "none";
- return true;
- }
-
- function saveSettings() {
- if (verifySettings()) {
- if (window.widget) {
- widget.setPreferenceForKey(nickname, widget.identifier + "_nickname");
-
- if (!checkForSavedServer(nickname)) saveServer();
- }
-
- document.getElementById("nickname_display").innerText = nickname;
- populateDropDown();
-
- toggleSettings();
- }
- }
-
- function toggleSettings() {
- var f = document.getElementById("front");
- var p = document.getElementById("settings");
-
- if (f.style.display != "none") {
- f.style.display = "none";
- p.style.display = "block";
-
- var bod = document.getElementsByTagName("body")[0];
- bod.removeEventListener("mouseover", mouseOver, true);
- bod.removeEventListener("mouseout", mouseOut, true);
-
- document.getElementById("flip").style.display = "none";
- if (!FTPDrop.isRegistered()) document.getElementById("register_buy").style.display = "block";
-
- if (window.widget) widget.prepareForTransition("ToBack");
-
- setTimeout(
- function() {
- if (nickname == "")
- document.getElementById("nickname").focus();
- else
- document.getElementById("password").focus();
-
- }, 800);
- } else {
- p.style.display = "none";
- f.style.display = "block";
-
- var bod = document.getElementsByTagName("body")[0];
- bod.addEventListener("mouseover", mouseOver, true);
- bod.addEventListener("mouseout", mouseOut, true);
-
- document.getElementById("flip").style.display = "block";
-
- if (window.widget) widget.prepareForTransition("ToFront");
- }
- if (window.widget) setTimeout("widget.performTransition()", 0);
- }
-
- //mouseover stuff for the 'i' button:
-
- var isShown = false, isAnimating = false;
-
- function mouseOver(e) {
- //e.cancelBubble = true;
-
- if (!isShown && !isAnimating) {
- //alert('yo');
- //document.getElementById("flip").style.display = "block";
- fade('flipper', 0, 1, function(){isAnimating=false;isShown = true;});
- isAnimating = true;
- }
-
- if (e.target.parentNode.id == "flip" || e.target.id == "flip") {
- document.getElementById("flip_back").style.visibility = "visible";
- }
- }
-
- function mouseOut(e) {
- //e.cancelBubble = true;
-
- if (e.target.id != "flipper" && e.target.parentNode.id != "flip") {
- document.getElementById("flip_back").style.visibility = "hidden";
- }
-
- //had a lot fo trouble with event capturing, so going this route for now...
- //we'll see what happens with actual Tiger...
- if (e.clientY < 0 || e.clientX < 0 || e.clientY > 250 || e.clientX > 230) {
- if (!isAnimating) {
- fade('flipper', 1, 0, function(){isAnimating=false;isShown = false;});
- isAnimating = true;
- }
- }
- }
-
- function infoMouseUp(e) {
- fade('flipper', 0, 0, function(){isAnimating=false;isShown = false;});
- document.getElementById("flip_back").style.visibility = "hidden";
- toggleSettings();
- }
-
- function fade(el, from, to, callBack) {
- if (el == new String(el)) el = document.getElementById(el);
- //so we can pass a string for the id or an element
-
- var dif = Math.abs(to - from);
-
- var pi = Math.PI;
- var theta = 0;
-
- //if (to < from && el.style.opacity > 0) return;
-
- var inter = setInterval(
- function() {
- if (theta < 90) {
- theta += 10;
- if (to > from)
- var step = Math.sin((theta*pi)/180);
- else
- var step = Math.cos((theta*pi)/180);
- var opac = dif*step;
- el.style.opacity = Math.max(0.01, opac); //fixing safari glitch
- } else {
- el.style.opacity = to;
- clearInterval(inter);
- if (callBack != null) callBack();
- }
- }, 100);
- }
-
- function toggleDropDown() {
- var dropdown = document.getElementById("nickname_choices");
-
- if (dropdown.style.display != "block") {
- dropdown.style.display = "block";
- } else {
- dropdown.style.display = "none";
- document.getElementById("nickname_button").src = "images/combo.png";
- }
- }
-
- function populateDropDown() {
- var dropdown = document.getElementById("nickname_choices");
-
- dropdown.innerHTML = "";
-
- var list = document.createElement("ul");
-
- var data = getSavedServers();
- var servers = data.getElementsByTagName("server");
-
- for (var i=0; i < servers.length; i++) {
- var server = document.createElement("li");
- server.appendChild(document.createTextNode(servers[i].getAttribute("nickname")));
- server.setAttribute("onclick", "getSavedServerInfo('" + servers[i].getAttribute("nickname") + "'); toggleDropDown()");
- list.appendChild(server);
- }
-
- dropdown.appendChild(list);
-
- dropdown.style.height = Math.min(16*servers.length, 100) + "px";
-
- }
-
- //help stuff:
- var help_running = false;
-
- function getOffsetTop(element) {
- var top = 0;
- var el = element;
-
- do {
- top += el.offsetTop;
- el = el.parentNode;
- } while (el !== document.body);
-
- return top;
- }
-
- function getOffsetLeft(element) {
- var left = 0;
- var el = element;
-
- do {
- left += el.offsetLeft;
- el = el.parentNode;
- } while (el !== document.body);
-
- return left;
- }
-
- var help_canvas_pen, help_canvas_x_stretch, help_canvas_y_stretch;
-
- function showHelp() {
- //this is basically an animation showing how to use the widget:
- help_running = true;
-
- //HAHA - the following is almost a joke, isn't it?
-
- document.getElementById("cover").style.display = "block";
- showHelpText(0,
- function () {
- help_canvas_pen = 1;
- help_canvas_y_stretch = 8;
- help_canvas_x_stretch = 10;
- circleObject('settings',
- function () {
- hideHelpText(0,
- function () {
- setTimeout(
- function() {
- showHelpText(1,
- function () {
- help_canvas_pen = 3;
- help_canvas_y_stretch = 1.5;
- help_canvas_x_stretch = 8;
- circleObject('nickname_combo',
- function () {
- setTimeout(
- function() {
- hideHelpText(1,
- function () {
- setTimeout(
- function() {
- fade("help_circle_canvas", 1.0, 0,
- function() {
- document.getElementById("help_circle_canvas").style.opacity = 1.0;
- document.getElementById("help_circle_canvas").style.display = "none";
- toggleSettings();
- setTimeout(
- function() {
- help_canvas_y_stretch = 4;
- help_canvas_x_stretch = 10;
- circleObject('drop_zone',
- function () {
- showHelpText(3,
- function () {
- setTimeout(
- function() {
- hideHelpText(3,
- function () {
- showHelpText(4,
- function () {
- help_canvas_y_stretch = 3;
- help_canvas_x_stretch = 7;
- circleObject('info',
- function() {
- setTimeout(
- function() {
- hideHelpText(4,
- function () {
- showHelpText(5,
- function () {
- setTimeout(
- function() {
- hideHelpText(5,
- function () {
- document.getElementById("help_circle_canvas").style.display = "none";
- document.getElementById("cover").style.display = "none";
- toggleSettings();
- }); //hideHelpText 5
- }, 3500);
- });//showHelpText 5
- });//hideHelpText 4
- }, 3500);
- });//circleObject 'drop_zone'
- });//showHelpText 4
- });//hideHelpText 3
- }, 3000);
- });//howHelpText 3
- });//circleObject 'front'
- }, 1000);
- }); //fade 'help_circle_canvas'
- }, 1000);
- });//hideHelpText 1
- }, 3500);
- });//circleObject 'nickname_combo'
- });//showHelpText 1
- }, 3500);
- });//hideHelpText 0
- });//circleObject 'settings'
- });//showHelpText 0
- }
-
- function cancelHelp() {
- help_running = false;
- document.getElementById("help_circle_canvas").style.display = "none";
- document.getElementById("help_text").style.display = "none";
- document.getElementById("cover").style.display = "none";
- }
-
- function handleKeyPress(e) {
- if (e.charCode == 27 && help_running) cancelHelp();
- }
-
- function circleObject(obj, callBack) {
- if (!help_running) return false;
-
- if (obj == new String(obj)) obj = document.getElementById(obj);
-
- var help_context = help_canvas.getContext("2d");
-
- help_canvas.style.display = "block";
-
- help_canvas.style.top = (getOffsetTop(obj) - 35) + "px";
- help_canvas.style.left = (getOffsetLeft(obj) - 35) + "px";
-
- help_context.lineWidth = 1; //help_canvas_pen;
- help_context.lineJoin = "bevel";
- help_context.lineCap = "round";
- help_context.strokeStyle = "rgba(255, 0, 0, 0.8)";
-
- help_context.clearRect(0, 0, 300, 300);
- help_context.save();
-
- help_context.scale(help_canvas_x_stretch, help_canvas_y_stretch);
-
- help_context.beginPath();
- help_context.moveTo(5, 10);
-
- var i=0;
-
- var inter = setInterval(
- function () {
- //alert(i);
- switch (i) {
- case 0:
- help_context.bezierCurveTo(5, 0, 20, 10, 20, 20);
- break;
- case 1:
- help_context.beginPath();
- help_context.moveTo(20, 10);
- help_context.bezierCurveTo(30, 20, 20, 30, 15, 20);
- break;
- case 2:
- help_context.beginPath();
- help_context.moveTo(20, 30);
- help_context.bezierCurveTo(10, 35, 5, 25, 5, 10);
- break;
- case 3:
- help_context.beginPath();
- help_context.moveTo(5, 25);
- help_context.bezierCurveTo(2, 7, 9, 10, 0, 0);
- break;
- //Default:
- case 4:
- clearInterval(inter);
- help_context.restore();
- if (callBack != null) callBack();
- break;
- }
- help_context.stroke();
- i++;
- }, 70);
- }
-
- function showHelpText(which, callBack) {
- if (!help_running) return false;
-
- var help_text = document.getElementById("help_text");
- help_text.style.display = "block";
-
- var messages = help_text.getElementsByTagName("div");
- if (which > 0) messages[which-1].style.opacity = 0;
-
- fade(messages[which], 0, 0.9,
- function() {
- //setTimeout("help_text_shadow.paint()", 100);
- callBack();
- });
- }
-
- function hideHelpText(which, callBack) {
- var help_text = document.getElementById("help_text");
-
-
- var messages = help_text.getElementsByTagName("div");
- //if (which > 0) messages[which-1].style.opacity = 0;
-
- fade(messages[which], 0.9, 0,
- function () {
- help_text.style.display = "none";
- callBack();
- });
- }
-
- var help_canvas;
-
- function register() {
- if (FTPDrop) FTPDrop.registerWidget();
- }
-
- function buy() {
- if (FTPDrop) FTPDrop.buyWidget();
- }
-
- var isNagging = false, flipped = false;
-
- function showNag(days_left) {
- //alert('days left (js): ' + days_left);
- isNagging = true;
- document.getElementById("front").style.display = "none";
- document.getElementById("settings").style.display = "none";
- window.resizeTo(341, 134);
-
- //I'll forgo doing this in a nice DOM way for the moment:
- var cont = document.createElement("div");
- cont.id = "buy_panel";
-
- var buy_p_text = document.createElement("div");
- buy_p_text.id = "buy_panel_text";
- cont.appendChild(buy_p_text);
-
- buy_p_text.innerHTML += '<span id="widget_name">FTP Drop</span> will run without any limitations for ' + days_left + ' more day';
- if (10 - days_left != 1) buy_p_text.innerHTML += 's'
- buy_p_text.innerHTML += '.<br />Thanks for trying it out!\n';
- var buy_p_buts = document.createElement("div");
- buy_p_buts.id = "buy_buttons";
- cont.appendChild(buy_p_buts);
-
- buy_p_buts.innerHTML += '<a href="#" onclick="FTPDrop.buyWidget()">Buy</a>\n';
- buy_p_buts.innerHTML += '<a href="#" onclick="FTPDrop.registerWidget()">Register</a>\n';
-
- var counter = document.createElement("span");
- counter.id = "counter";
- counter.innerText = 7;
-
- buy_p_buts.appendChild(counter);
-
- document.body.appendChild(cont);
-
- var count_down = 7;
-
- var inter = setInterval(
- function () {
- if (--count_down >= 0) {
- counter.innerText = count_down;
- } else {
- clearInterval(inter);
- window.resizeTo(250, 230);
- if (!flipped) {
- document.getElementById("front").style.display = "block";
- toggleSettings();
- } else {
- document.getElementById("settings").style.display = "block";
- document.getElementById("register_buy").style.display = "block";
- }
-
- document.body.removeChild(cont);
- }
- }, 1000);
- }
-
- function init() {
- if (window.widget) {
- //alert(widget.identifier);
-
- nickname = widget.preferenceForKey(widget.identifier + "_nickname") || "";
- }
-
- if (nickname != "") {
- if (checkForSavedServer(nickname)) {
- getSavedServerInfo(nickname);
- }
- }
-
- populateDropDown();
-
- //set up the flip ('i') button:
- var bod = document.getElementsByTagName("body")[0];
- bod.addEventListener("mouseover", mouseOver, true);
- bod.addEventListener("mouseout", mouseOut, true);
- document.getElementById("flip").addEventListener("mouseup", infoMouseUp, false);
-
- var drop_zone = document.getElementById("drop_zone");
- drop_zone.addEventListener("drop", getFile, true);
- drop_zone.addEventListener("dragenter", doDragEnter, true);
- drop_zone.addEventListener("dragover", doDragMove, true);
- drop_zone.addEventListener("dragleave", doDragLeave, true);
-
- setTimeout(
- function () {
- if (!isNagging) {
- flipped = true;
- toggleSettings();
- }
- }, 500);
-
- document.addEventListener("keydown", handleKeyPress, false);
- help_canvas = document.getElementById("help_circle_canvas");
- }
-
- window.onload = init;
- if (window.widget) {
- widget.onremove =
- function() {
- //clear up after ourselves
- widget.setPreferenceForKey(null, widget.identifier + "_nickname");
- };
- }